<?php
/**
 * Handles the Jabberdrive network
 * @author Gustav Knutsson
 */
class jdNetworkMeta{
	protected $jddb;
	
	/**
	 * Class construct
	 */
	public function __construct(){
		$this->jddb = jdDatabaseMySql::getInstance();
	}
	
	/**
	 * returns the next node ID to store on, it uses the amount of space left 
	 * as an operand to decide where to save the file
	 * 
	 * @return INT
	 * 
	 * @author Gustav Knutsson
	 */
	public function getNextNode(){
		$nodes = $this->getNodeList();
		$i=0;
		$n["id"] 	= 0;// Self
		$n["size"] 	= disk_free_space(JABBERDRIVE_STORAGE_AREA);
		foreach($nodes as $node){
			if($node["n_storage_size_left"] > $n["size"]){
				$n["id"] 	= $node["n_id"];
				$n["size"] 	= $node["n_storage_size_left"];
			}
		}
		
		return $n["id"];
	}
	
	/**
	 * Returns array of nodes.
	 * @author Gustav Knutsson
	 */
	public function getNodeList(){
		if($this->thisType() == 'slave') 
			return array();
			
		return $this->jddb->fetch("SELECT * FROM nodes");
	}
	
	/**
	 * Returns the type of which this instance of jabberdrive is, eg. master or slave
	 * @author Gustav Knutsson
	 */
	public function thisType(){
		return strtolower(JABBERDRIVE_MASTER_SLAVE);
	}
}
?>